//
//  ParticipateService.m
//  homeland
//
//  Created by ops on 14-5-12.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "ParticipateService.h"
#import "RecommendListItem.h"

@implementation ParticipateService
static ParticipateService *participate;
+(ParticipateService *)share
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        participate = [[ParticipateService alloc]init];
    });
    return participate;
}


- (void)isParticipate
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    NSString *userName = [DataStoreService loadFromKeychain:AUTO_LOGIN_USER_NAME];
    //    NSString *userPass = [DataStoreService loadFromKeychain:AUTO_LOGIN_USER_PASSWORD];
    NSString *userId = [DataStoreService loadFromKeychain:LOGIN_USER_ID];

    [params setValue:[NSNumber numberWithInt:[userId intValue]] forKey:@"userId"];
    
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:IsParticipate_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:@"" params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        
        NSDictionary *json = [completedOperation responseJSON];
        BOOL isSuccess = ((NSNumber*)[json objectForKey:@"isSuccess"]).boolValue;
        if (self.delegate && [self.delegate respondsToSelector:@selector(isParticipate:)]) {
            [self.delegate isParticipate:isSuccess];
        }

    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {

    }];
    [currentEngine enqueueOperation:op];
}

- (void)subParticipate
{
    
    NSString *userName = [DataStoreService loadFromKeychain:LOGIN_USER_NAME];
    NSString *userId = [DataStoreService loadFromKeychain:LOGIN_USER_ID];
    
    NSLog(@"userName = %@",userName);
    NSLog(@"userId = %@",userId);


    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSNumber numberWithInt:[userId intValue]] forKey:@"userId"];
    [params setValue:userName forKey:@"userName"];
    [params setValue:[NSNumber numberWithInt:1001] forKey:@"activityId"];
    [params setValue:@"《参与有奖活动》" forKey:@"activityExplanation"];

    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:ParticipateActivity_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:@"" params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        
        NSDictionary *json = [completedOperation responseJSON];
        BOOL isSuccess = ((NSNumber*)[json objectForKey:@"isSuccess"]).boolValue;
        if (self.delegate && [self.delegate respondsToSelector:@selector(subSuccess:)]) {
            [self.delegate subSuccess:isSuccess];
        }
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        
    }];
    [currentEngine enqueueOperation:op];

}

- (void)getRecommendList:(NSString *)mobile pageNO:(NSString *)pageNO
{
    
    NSString *userName = [DataStoreService loadFromKeychain:LOGIN_USER_NAME];
    NSString *userId = [DataStoreService loadFromKeychain:LOGIN_USER_ID];

    NSLog(@"userName = %@",userName);
    NSLog(@"userId = %@",userId);

    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSNumber numberWithInt:[userId intValue]] forKey:@"userId"];
    if (mobile != nil && [mobile length] > 0) {
        [params setValue:mobile forKey:@"mobile"];
    }
    
    NSString *url = [GetRecommendList_URL stringByAppendingString:pageNO];
    url = [url stringByAppendingString:@"?"];
    
    NSLog(@"url = %@",url);
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:url customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:@"" params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        
        NSDictionary *json = [completedOperation responseJSON];
        BOOL isHasNext = ((NSNumber*)[json objectForKey:@"hasNextPage"]).boolValue;
    
        NSArray *array = [json objectForKey:@"pageList"];
        [self.delegate recommendList:array hasNext:isHasNext];

        
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        
    }];
    [currentEngine enqueueOperation:op];
    
}


- (void)subRecommender:(NSString *)mobile
{
    
    NSString *userName = [DataStoreService loadFromKeychain:LOGIN_USER_NAME];
    NSString *userId = [DataStoreService loadFromKeychain:LOGIN_USER_ID];
    
    NSLog(@"userName = %@",userName);
    NSLog(@"userId = %@",userId);
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSNumber numberWithInt:[userId intValue]] forKey:@"userId"];
    [params setValue:userName forKey:@"userName"];

    if (mobile != nil && [mobile length] > 0) {
        [params setValue:mobile forKey:@"mobileList"];
    }
    

    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:AddRecommendList_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:@"" params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        
        NSDictionary *json = [completedOperation responseJSON];
        NSString *message = [json objectForKey:@"message"];        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [self.delegate subSuccess:YES];
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        
    }];
    [currentEngine enqueueOperation:op];
    
}

@end
